package com.edu.zut.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.zut.domain.MenuVo;
import com.edu.zut.entity.Menu;
import com.edu.zut.entity.RoleMenu;
import com.edu.zut.entity.User;
import com.edu.zut.mapper.MenuMapper;
import com.edu.zut.mapper.RoleMapper;
import com.edu.zut.mapper.RoleMenuMapper;
import com.edu.zut.mapper.UserMapper;
import com.edu.zut.result.HttpCode;
import com.edu.zut.result.ResponseResult;
import com.edu.zut.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author 许文科
 * @since 2023-01-12
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private MenuMapper menuMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 根据parent_id查询菜单列表
     *
     * @param parentId
     * @return
     */
    @Override
    public List<MenuVo> selectMenusByParentId(Long parentId) {
        List<MenuVo> menus = menuMapper.selectMenusByParentId(parentId);
        return menus;
    }

    /**
     * 根据用户名查出该用户所拥有的菜单列表
     *
     * @param username
     * @return
     */
    @Override
    public List<MenuVo> selectMenuTreeByUserName(String username) {

        //查出当前用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", username);
        User user = userMapper.selectOne(wrapper);
        //System.out.println("user: " + user);

        // 根据username判断用户role
        //List<String> roleNames = roleMapper.getRoleNamesbyUserId(user.getUserId());
        Long roleId = null;
        if (user.getUserType().equals("0")) {
            roleId = roleMapper.getRoleIdByRoleName("ROLE_admin");
        } else if (user.getUserType().equals("1")) {
            roleId = roleMapper.getRoleIdByRoleName("ROLE_teacher");
        } else {
            roleId = roleMapper.getRoleIdByRoleName("ROLE_student");
        }

        //System.out.println("roleId: " + roleId);


        List<Menu> menuList = null;

        menuList = menuMapper.selectMenuTreeByRoleId(roleId);
        return transferMenuVo(menuList, 0L);
    }

    /**
     * 构建权限树
     *
     * @param allMenu
     * @param parentId
     * @return
     */
    private List<MenuVo> transferMenuVo(List<Menu> allMenu, Long parentId) {
        List<MenuVo> menuVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(allMenu)) {
            for (Menu source : allMenu) {
                if (parentId.longValue() == source.getParentId().longValue()) {
                    MenuVo menuVo = new MenuVo();
                    BeanUtils.copyProperties(source, menuVo);
                    //递归查询子菜单，并封装信息
                    List<MenuVo> childList = transferMenuVo(allMenu, source.getMenuId());
                    if (!CollectionUtils.isEmpty(childList)) {
                        menuVo.setChildren(childList);
                    }
                    menuVoList.add(menuVo);
                }
            }
        }
        return menuVoList;
    }

    //TODO 添加菜单功能
    @Override
    public ResponseResult addMenu(Menu menu) {
        //如果插入的当前节点为根节点，parendId = 0
        if (menu.getParentId().longValue() == 0) {
            menu.setPath(null);
        } else {
            //父菜单
            Menu parentMenu = menuMapper.selectById(menu.getParentId());
            if (parentMenu == null) {
                throw new RuntimeException("未查询到对应的父节点");
            }

        }
        return null;
    }

    @Override
    public ResponseResult queryRouter(String username) {

        List<Menu> menus = menuMapper.selectList(null);
        if (menus.isEmpty()) {
            return new ResponseResult(HttpCode.ERROR, "失败.", menus);
        }
//        System.out.println("queryRouter: " + menus.toArray());
        return new ResponseResult(HttpCode.SUCCESS, "成功.", menus);
    }

    @Override
    public ResponseResult getPermTree() {
        /**
         * 构建权限树，
         */

        //首先得到所有的权限信息
        List<Menu> menuList = menuMapper.selectmenuTreeAll();
        List<MenuVo> menuVoList = transferMenuVo(menuList, 0L);

        return new ResponseResult(HttpCode.SUCCESS, "成功构建权限树.", menuVoList);
    }

    @Override
    public ResponseResult getRoleTeacherPerm() {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", 2L);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(wrapper);
        if (!roleMenus.isEmpty()) {
            return new ResponseResult(HttpCode.SUCCESS, "成功.", roleMenus);
        }
        return new ResponseResult(HttpCode.ERROR, "失败.", null);

    }

    @Override
    public ResponseResult getRoleStudentPerm() {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", 3L);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(wrapper);
        if (!roleMenus.isEmpty()) {
            return new ResponseResult(HttpCode.SUCCESS, "成功.", roleMenus);
        }
        return new ResponseResult(HttpCode.ERROR, "失败.", null);

    }

}
